﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAO;
using DTO;

namespace BUS
{
    public static class CongVanDenBUS
    {
        public static bool Add(CongVanDenDTO congVanDen)
        {
            return CongVanDenDAO.Add(congVanDen);
        }
        public static bool AddRelationship(CongVanDenDTO congVanDen)
        {
            return CongVanDenDAO.AddRelationship(congVanDen);
        }
        public static bool DeleteRelationship(int id)
        {
            return CongVanDenDAO.DeleteRelationship(id);
        }
        public static int Count(int maLoai)
        {
            return CongVanDenDAO.Count(maLoai);
        }
        public static CongVanDenDTO Load(int id)
        {
            CongVanDenDTO tmp =  CongVanDenDAO.Load(id);
            tmp.DonVi= DonViBUS.Load(id);
            return tmp;
        }
        public static bool Edit(CongVanDenDTO congVanDen)
        {
            return CongVanDenDAO.Edit(congVanDen);
        }


        public static DataTable TraCuu(CongVanDenSearchDTO congVanDenSearchDTO)
        {
            try
            {
                return CongVanDenDAO.TraCuu(congVanDenSearchDTO);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static bool Luu(String path, DataTable data)
        {
            try
            {
                return CongVanDenDAO.Luu(path, data);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static DataTable ThongKe(int nam, bool theoThang)
        {
            DataTable result=new DataTable();
            int[,] arr;
            DataColumn c1;
            if (theoThang)
            {
                c1 = new DataColumn("Tháng");
            }
            else
            {
                c1 = new DataColumn("Quý");
            }
            c1.DataType = System.Type.GetType("System.Int32");
            result.Columns.Add(c1); 
            DataColumn c2;
            c2 = new DataColumn("Tổng");
            c2.DataType = System.Type.GetType("System.Int32");
            result.Columns.Add(c2);

            List<Tuple<int, int, int>> tmp = CongVanDenDAO.ThongKe(nam, theoThang);
            List<LoaiDenDTO> lstLoai = LoaiDenBUS.LoadAll();
            Dictionary<int, int> dct = new Dictionary<int, int>();

            for (int i = 0; i < lstLoai.Count; i++ )
            {
                DataColumn c = new DataColumn(lstLoai[i].Ten);
                c1.DataType = System.Type.GetType("System.Int32");
                result.Columns.Add(c);
                dct.Add(lstLoai[i].ID, i);

            }
            if (theoThang)
            {
               arr = new int[12,lstLoai.Count+2];
               for (int i = 0; i < 12; i++)
               {
                   for (int j = 0; j < lstLoai.Count + 2; j++)
                   {
                       arr[i, j] = 0;
                   }
               }
            }
            else
            {
                arr = new int[4, lstLoai.Count + 2];
                for (int i = 0; i < 4; i++)
                {
                    for (int j = 0; j < lstLoai.Count + 2; j++)
                    {
                        arr[i, j] = 0;
                    }
                }
            }

            
            foreach (Tuple<int, int, int> t in tmp)
            {
                arr[t.Item1-1, dct[t.Item2]+2] += t.Item3;
                arr[t.Item1 - 1, 0] += t.Item3;
            }
            if (theoThang)
            {
                for (int i = 0; i < 12; i++)
                {
                    DataRow r = result.NewRow();
                    r.SetField(0, i + 1);
                    for (int j = 0; j < lstLoai.Count+1; j++)
                    {
                        r.SetField(j+1,arr[i, j]);
                    }
                    result.Rows.Add(r);
                }
            }
            else
            {
                for (int i = 0; i < 4; i++)
                {
                    DataRow r = result.NewRow();
                    r.SetField(0, i + 1);
                    for (int j = 0; j < lstLoai.Count + 1; j++)
                    {
                        r.SetField(j + 1, arr[i, j]);
                    }
                    result.Rows.Add(r);
                }
            }
            
            return result;
        }
    }
}
